import Toast from 'tdesign-miniprogram/toast';
import {
  getDetail
} from '../../services/activity/index';
import {
  joinActivity,
  exitActivity
} from '../../services/activity/index'
Page({
  data: {
    banner: '',
    time: 0,
    showBannerDesc: false,
    statusTag: '',
    detail: {},
    openId: wx.getStorageSync('openid'),
    activityId: ''
  },

  onLoad(query) {
    const id = parseInt(query.activityId);
    this.setData({
      activityId: id
    })
  },
  onShow() {
    const openId = wx.getStorageSync('openid');
    if (openId) {
      this.setData({
        openId
      })
    }
    this.getList(this.data.activityId)
  },
  getList(id) {
    getDetail(id, this.data.openId).then((res) => {
      if (res.data.code == 200) {
        const obj = res.data.data
        if (obj.content) {
          obj.content = obj.content.replace(/<img/gi, '<img style="width:100%;height:auto;margin:0 auto;display:block"')
        }
        this.setData({
          detail: obj,
          activityId: obj.activityId
        })
        console.log(this.data)
      }
    });
  },
  getNoticeList() {
    // 获取校友会下所有的活动

  },
  goodClickHandle(e) {
    const {
      index
    } = e.detail;
    const {
      spuId
    } = this.data.list[index];
    wx.navigateTo({
      url: `/pages/goods/details/index?spuId=${spuId}`
    });
  },
  joinUs() {
    const openId = this.data.openId
    if (!openId) {
      wx.switchTab({
        url: '/pages/usercenter/index'
      })
    } else {
      const {
        hasMe,
        activityId,
        associationId
      } = this.data.detail
      if (hasMe) {
        exitActivity({
          openId,
          activityId,
          associationId
        }).then(res => {
          if (res.data.code == 200) {
            Toast({
              context: this,
              selector: '#t-toast',
              message: '退出成功',
            });
            this.getList(this.data.activityId)
          }
        })
      } else {
        joinActivity({
          openId,
          activityId,
          associationId
        }).then(res => {
          if (res.data.code == 200) {
            Toast({
              context: this,
              selector: '#t-toast',
              message: '退出成功',
            });
            this.getList(this.data.activityId)
          }
        })
      }
    }
  }
});